package applogic;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Room {
	int romID;
	ArrayList<Long[]> reserved;
	int size;
	
	public Room(int romID,int size)
	{
		this.romID = romID;
		this.size = size;
		this.reserved = new ArrayList<Long[]>();
	}
	
	public int getID()
	{
		return romID;
	}
	
	boolean isAvailable(Long start, Long end) {
		for(Long[] reservation:reserved)
		{
			if((reservation[0]<start&&reservation[1]>start)
				||(reservation[0]<end&&reservation[1]>end))return false;
			if(reservation[0]==start&&reservation[1]==start)return false;
		}
		return true;
	}
	
	public void cancelRerservation(Meeting meeting) {
		long start,end;
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_MONTH, meeting.getDay());
		c.set(Calendar.MONTH,meeting.getMonth()-1);
		c.set(Calendar.HOUR_OF_DAY,meeting.getStart());
		start = c.getTimeInMillis();
		c.set(Calendar.HOUR_OF_DAY,meeting.getEnd());
		if(meeting.getEnd()<meeting.getStart())c.set(Calendar.DAY_OF_MONTH, meeting.getDay()+1);
		end = c.getTimeInMillis();
		Long[] match = null;
		for(Long[] reservation:reserved)
		{
			if(reservation[0]==start&&reservation[1]==end)
			{
				match = reservation;
				break;
			}
		}
		if(match!=null)reserved.remove(match);
	}
	
	public void reserve(Meeting meeting)
	{
		long start,end;
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_MONTH, meeting.getDay());
		c.set(Calendar.MONTH,meeting.getMonth()-1);
		c.set(Calendar.HOUR_OF_DAY,meeting.getStart());
		start = c.getTimeInMillis();
		c.set(Calendar.HOUR_OF_DAY,meeting.getEnd());
		if(meeting.getEnd()<meeting.getStart())c.set(Calendar.DAY_OF_MONTH, meeting.getDay()+1);
		end = c.getTimeInMillis();
		reserved.add(new Long[]{start,end});
	}
	
	public int getSize()
	{
		return size;
	}
	
	public String toString(){
		return "RomID = "+romID+" Capacity= "+ size ;
		
	}
}
